package cn.edu.hunau.java.week03.day05;

import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class EmployeeTest {

    @Test
    public void testWrite(){
        Employee e1=new Employee(1001,"张麻子",3000.0,"程序员","12345678901", LocalDate.of(2020,1,1));
        try(FileOutputStream fos=new FileOutputStream("dir/emps.text",true);
            PrintWriter pw=new PrintWriter(fos,true, Charset.forName("UTF-8"));){
            pw.println(e1);
        }catch(Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    @Test
    public void testWrite2(){
        Employee e1=new Employee(1002,"马邦德",2500.0,"程序员","12345678902",LocalDate.of(2020,1,1),1001);
        EmpManagerService service=new EmpManagerService();
        service.writerEmployeeInfo(e1);
    }

    @Test
    public void testreadEmps(){
        List<Employee> emps=new ArrayList<>();
        try(FileInputStream fis=new FileInputStream("dir/emps.text");
            InputStreamReader isr=new InputStreamReader(fis,"utf-8");
            BufferedReader br=new BufferedReader(isr);){
            String line=null;
            while((line=br.readLine())!=null){
                Employee emp=parseEmployee(line);
                emps.add(emp);
            }
        }catch(Exception e){
            throw new RuntimeException(e.getMessage());
        }
        System.out.println(emps.size());
        System.out.println(emps);
    }
    private Employee parseEmployee(String line){
        String[] datas=line.split(":");
        Employee emp=new Employee();
        emp.setId(Integer.parseInt(datas[0]));
        emp.setName(datas[1]);
        emp.setSalary(Double.parseDouble(datas[2]));
        emp.setJob(datas[3]);
        emp.setTelphone(datas[4]);
        emp.setIntoDate(LocalDate.parse(datas[5]));
        emp.setManagerId("null".equals(datas[6])?null:Integer.parseInt(datas[6]));
        return emp;
    }

    @Test
    public void testLoadDatas(){
        EmpManagerService service=new EmpManagerService();
    }
    EmpManagerService service=new EmpManagerService();

    @Test
    public void testJobs(){
        Set<String> jobs=service.jobs();
        System.out.println(jobs);
        System.out.println(jobs.size());
    }

    @Test
    public void getEmpId(){
        Integer empId=service.getEmpId();
        System.out.println(empId);
    }

    @Test
    public void getManagers(){
        Map<Integer,List<Employee>> manager=service.manager();
        Set<Integer> ids=manager.keySet();
        System.out.println(ids);
    }
}
